function ispec(varargin)
% plot spectrum of current traces --- 
% 29 Sept. 2003 P. Manis
%
% updated 17 Sept. 2005. 
% new parameters for welch.
% twin is a two-element array indicateing the beginning and ending times
% for the spectral calculation.

global CURRENT VOLTAGE DFILE

rate = DFILE.nr_channel(1)*DFILE.rate(1);
fs = 1000000/rate; % in Hertz.
if(nargin == 0 | isempty(varargin{1}))
    hf = findobj('Tag', 'I_Spec');
    if(isempty(hf))
        hf=figure('Tag', 'I_Spec');
    else
        figure(hf);
        clf;
    end;
else
    axes(varargin{1});
end;
t0 = 1;
t1 = size(CURRENT,2);
r = 0.001*DFILE.rate(1)*DFILE.nr_channel(1);
if(nargin >= 2)
    twin = varargin{2}; % get the argument.
    t0 = floor(twin(1)/r);
    t1 = floor(twin(2)/r);
end;
    
n = size(CURRENT, 1);

avs = [];
for i = 1:n
   [sp, fr] = pwelch(CURRENT(i,t0:t1)-mean(CURRENT(i,t0:t1)), 1024,256, 4096, fs);
   if(i == 1)
      avs = sp;
   else
      avs = avs + sp;
   end;
   
end;
avs = avs/n;
loglog(fr(1:end), avs(1:end), 'b');
if(nargin == 3)
vavs = [];
for i = 1:n
   [vsp, vfr] = pwelch(VOLTAGE(i,t0:t1)-mean(VOLTAGE(i,t0:t1)), 1000, 0, 5000, fs);
   if(i == 1)
      vavs = vsp;
   else
      vavs = vavs + vsp;
   end;
   
end;
vavs = vavs/n;
hold on;
loglog(vfr(1:end), vavs(1:end), 'k');
end;


return;


